﻿//using NuGet.Packaging;
using MiniExcelLibs;
using PanhuaWin.系统管理;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PanhuaWin.仓库.下线
{
    public partial class Form岳阳标签临时打印 : UIPage
    {
        public string 工厂编号1;
        BarTender.Application ba = new BarTender.Application();
        BarTender.Format bfmt = new BarTender.Format();
        public Form岳阳标签临时打印()
        {
            InitializeComponent();
        }

        private void Form大标签打印_Initialize(object sender, EventArgs e)
        {

        }

        private void Form大标签打印_Load(object sender, EventArgs e)
        {
            uiComboBox加工类别.Items.Add("镀锌");
            uiComboBox加工类别.Items.Add("彩涂");
            uiComboBox加工类别.Items.Add("冷轧");//罩退的
            uiComboBox加工类别.Items.Add("彩基板");

            //包装
            uiComboBox包装.Items.Add("精包");
            uiComboBox包装.Items.Add("简包");
            uiComboBox包装.Items.Add("普包");
            uiComboBox包装.Items.Add("裸包");
            uiComboBox包装.Items.Add("无");
            this.uiDatePicker报工日期.Value = DateTime.Now;
            this.uiDatePicker1.Value = DateTime.Now;
            this.uiDatePicker2.Value = DateTime.Now;
            pub.ListWorkShop(pub.sys_user, uiComboBoxWorkshop);
            this.uiTextBox班组.Text = pub.Get员工所属班组(pub.sys_user.UserTrueName);
            pub.ListRank(pub.全局工厂编号, this.uiComboBox等级);
            //列出没有打印大标签的小标签
            未打印();
        }

        private void 未打印()
        {
            decimal 总重量;
            decimal 总长度;
            int 总卷数;
            总重量 = 0;
            总长度 = 0;
            string sql = @"select guid,来源种类,来源产线, 工厂编号, 生产批号,  品名, 锌层,钢号,包装形式, 条码, 长度, 规格,班组, 等级, 表面状态, 仓库 , 表面处理,";
            sql += "    净重 ,'' 文本净重, 备注, 生产日期,操作人员,操作时间,颜色 from Bus_LablePrint where 1=1 ";
            if (!this.checkBox包含已经打印大标签.Checked)
            {
                //sql += " and  生产批号 not in (select VolumeCodeNew from Bus_Volume) ";
                //sql += " and  生产批号 not in (select VolumeCodeNew from Bus_StockInVolume) ";
                sql += " and 是否已打印 = '否' ";

            }
            //if (this.checkBox2.Checked)
            //{
            //    sql += " and 来源产线 ='" + this.uiComboBox查询产线.SelectedText + "'";
            //}
            //if (this.checkBox1.Checked)
            //{
            //    sql += " and 班组 ='" + this.uiTextBox班组.Text + "'";
            //}
          
                sql += " and 生产日期>='"+ this.uiDatePicker1.Value.ToString("yyyy-MM-dd") +"'";
            sql += " and 生产日期<'" + this.uiDatePicker2.Value.AddDays(1).ToString("yyyy-MM-dd") + "'";
            
            sql +=" order by [操作时间] desc ; ";

            DataTable dt = pub.GetDataTable(pub.ConnectionString,sql);
            uiDataGridView1.AutoGenerateColumns=false; ;
            uiDataGridView1.AutoSizeColumnsMode=DataGridViewAutoSizeColumnsMode.Fill;
           
            if (dt.Rows.Count >0)
            {
                for (int i= 0; i < dt.Rows.Count; i++)
                {
                    decimal 净重  = pub.Todecimal(dt.Rows[i]["净重"].ToString());
                    dt.Rows[i]["文本净重"] = Math.Round( 净重,0).ToString();
                    总重量 += 净重;
                    总长度 += pub.Todecimal(dt.Rows[i]["长度"].ToString());
                }
            }
            uiDataGridView1.DataSource = dt;
            uiDataGridView1.Refresh();
            this.uiTextBox总卷数.Text = dt.Rows.Count.ToString();
            this.uiTextBox总重量.Text = Math.Round( 总重量,0).ToString();
            this.uiTextBox总米数.Text = 总长度.ToString();
        }

        private void uiComboBox加工类别_SelectedIndexChanged(object sender, EventArgs e)
        {
            string 加工类别;
            加工类别 = this.uiComboBox加工类别.Text;
            if (加工类别 == "镀锌")
            {
                List短品名(this.uiComboBox品名, 加工类别);
                this.uiLabel前缀.Text = "PHD";
                //pub.ListWorkLine(this.uiComboBoxWorkshop.SelectedValue.ToString(), "镀锌", this.uiComboBox产线);
                //pub.ListWorkLine(this.uiComboBoxWorkshop.SelectedValue.ToString(), "镀锌", this.uiComboBox查询产线);
                //加工类别 镀锌 需要加上彩度的产线
                ListWorkLine(this.uiComboBoxWorkshop.SelectedValue.ToString(), "镀锌", "彩涂", this.uiComboBox产线);
                ListWorkLine(this.uiComboBoxWorkshop.SelectedValue.ToString(), "镀锌", "彩涂", this.uiComboBox查询产线);
            }
            if (加工类别 == "彩涂")
            {
                List短品名(this.uiComboBox品名, 加工类别);
                this.uiLabel前缀.Text = "PHC";
                pub.ListWorkLine(this.uiComboBoxWorkshop.SelectedValue.ToString(), "彩涂", this.uiComboBox产线);
                pub.ListWorkLine(this.uiComboBoxWorkshop.SelectedValue.ToString(), "彩涂", this.uiComboBox查询产线);

            }

            if (加工类别 == "冷轧")
            {
                List短品名(this.uiComboBox品名, 加工类别);
                this.uiLabel前缀.Text = "PHZ";
                pub.ListWorkLine(this.uiComboBoxWorkshop.SelectedValue.ToString(), "罩退", this.uiComboBox产线);
                pub.ListWorkLine(this.uiComboBoxWorkshop.SelectedValue.ToString(), "罩退", this.uiComboBox查询产线);

            }
            if (加工类别 == "彩基板")
            {
                List短品名(this.uiComboBox品名, 加工类别);
                this.uiLabel前缀.Text = "PHD";
                pub.ListWorkLine(this.uiComboBoxWorkshop.SelectedValue.ToString(), "镀锌", this.uiComboBox产线);
                pub.ListWorkLine(this.uiComboBoxWorkshop.SelectedValue.ToString(), "镀锌", this.uiComboBox查询产线);
            }


            if (加工类别 == "冷轧")
            {
                List短品名(this.uiComboBox品名, 加工类别);
                加工类别 = "罩退";
            }
            pub.ListMaterial(this.uiComboBoxWorkshop.SelectedValue.ToString(), 加工类别, this.uiComboBox钢号);
          
            pub.ListFaceDone(加工类别, this.uiComboBox表面处理);

        }

        private void ListWorkLine(string 工厂编号, string workType,string workType2, UIComboBox uiComboBox产线)
        {
            string sql;
            sql = "SELECT [guid]     ,[WorkShopCode]     ,[DepartGuid]     ,[DepartCode]     ,[WorkLineCode]     ,[WorkLineName]     ,[AuditDate]     ,[AuditStatus]     ,[AuditId]     ,[Auditor]     ,[Remark]     ,[CreateID]     ,[Creator]     ,[CreateDate]     ,[ModifyID]     ,[Modifier]     ,[ModifyDate] FROM [Dic_WorkLine] ";
            sql += " where WorkShopCode='" + 工厂编号 + "'  ";
            if (workType != "退火")
            {
                sql += " and workType='" + workType + "' or workType = '"+ workType2 + "'";
            }
            else
            {
                sql += " and WorkLineName='二期镀退一线'";
            }



            sql += "  order by WorkLineCode  ";

            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb != null && tb.Rows.Count > 0)
            {
                uiComboBox产线.DataSource = tb;
                uiComboBox产线.DisplayMember = "WorkLineName";
                uiComboBox产线.ValueMember = "WorkLineCode";

                uiComboBox产线.Refresh();

            }
            else
            {

                uiComboBox产线.DataSource = null;
                uiComboBox产线.Refresh();
            }
        }

        private  void List短品名(UIComboBox uiComboBox品名, string 品名)
        {
            string sql;
            if (品名 == "彩基板")
            {
                
                sql = "select distinct[INVSHORTNAME] from[Dic_Inventory] where[INVCLASSNAME] = '彩基板'";
            }
            else
            {
                sql = "select distinct INVSHORTNAME from Dic_Inventory where INVNAME like '%" + 品名 + "%' order by INVSHORTNAME ";
            }
           
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                uiComboBox品名.DataSource = tb;
                uiComboBox品名.DisplayMember = "INVSHORTNAME";
                uiComboBox品名.ValueMember = "INVSHORTNAME";
                uiComboBox品名.Refresh();
            }
            else
            {
                uiComboBox品名.DataSource = null;

                uiComboBox品名.Refresh();

            }
        }

        private void Form大标签录入_LocationChanged(object sender, EventArgs e)
        {

        }

        private void uiComboBox加工类别_SelectedIndexChanged_1(object sender, EventArgs e)
        {

        }

        private void uiTextBox新厚度_TextChanged(object sender, EventArgs e)
        {
            SetSize();
        }

        private void SetSize()
        {
            this.uiTextBox规格.Text = pub.Todecimal(this.uiTextBox新厚度.Text, 3) + "*" + pub.ToInt(this.uiTextBox新宽度.Text) + "*C";

        }

        private void uiTextBox新宽度_TextChanged(object sender, EventArgs e)
        {
            SetSize();
        }

        private void uiComboBoxWorkshop_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            pub.ListRank(this.uiComboBoxWorkshop.SelectedValue.ToString(), uiComboBox等级);
            string sql;
           工厂编号1 = this.uiComboBoxWorkshop.SelectedValue.ToString();
            sql = @"SELECT   guid, WorkShopCode, WorkShopName, AuditDate, AuditStatus, AuditId, Auditor, Remark, CreateID, Creator, 
                CreateDate, ModifyID, Modifier, ModifyDate   FROM      Dic_WorkShop     where  WorkShopCode='" + 工厂编号1 + "'      ";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                //                sql = @"SELECT    guid, WorkShopGuid, WarehouseCode, WarehouseName, WarehouseDesc, AuditDate, AuditStatus, AuditId, 
                //                Auditor, Remark, CreateID, Creator, CreateDate, ModifyID, Modifier, ModifyDate
                //FROM      Dic_Warehouse where WorkShopGuid ='" + tb.Rows[0]["guid"].ToString() + "'";
                //                DataTable tb2 = pub.GetDataTable(pub.ConnectionString, sql);
                //pub.ListWareHouse(this.uiComboBox仓库编号1, tb.Rows[0]["guid"].ToString());
                //if (this.uiComboBox仓库编号1.SelectedValue.ToString() != "")
                //{
                //    Do库位刷新1(this.uiComboBox仓库编号1.SelectedValue.ToString());
                //}

            }
        }

        private void Do库位刷新1(string 仓库编号)
        {
            string sql;

            sql = @"SELECT   guid, WorkShopCode, WorkShopName, AuditDate, AuditStatus, AuditId, Auditor, Remark, CreateID, Creator, 
                CreateDate, ModifyID, Modifier, ModifyDate   FROM      Dic_WorkShop     where  WorkShopCode='" + 工厂编号1 + "'      ";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                this.uiComboBox入库库位1.DataSource = null;
                uiComboBox入库库位1.Refresh();
                pub.ListLocation(this.uiComboBox入库库位1, 工厂编号1, this.uiTextBox库位搜索.Text, 仓库编号);
            }
        }

        private void uiButton清空_Click_1(object sender, EventArgs e)
        {

            this.uiTextBox表面状态.Text = "";
            //this.uiTextBox品名.Text = "";
            this.uiTextBox备注.Text = "";
            this.uiTextBox生产批号.Text = "";
            //this.uiTextBox表面处理.Text = "";
            this.uiTextBox净重.Text = "";
            this.uiTextBox规格.Text = "";
            this.uiTextBox长度.Text = "";
        }

        private void uiButton打印_Click_1(object sender, EventArgs e)
        {
            //要从 酸洗报工  轧硬报工  产出的

            string sql;
            //验证一下信息
            string 生产批号;
            string 品名;
            string 钢号;
            string 长度;
            string 规格;
            string 等级;
            string 表面状态;
            string 表面处理;
            string 净重;
            string 备注;
            string 生产日期;
            //string 仓库 = uiComboBox仓库编号1.SelectedValue?.ToString();
            string 仓库 = "";
            string 锌层 = uiTextBox锌层.Text;
            string 包装形式 = uiComboBox包装.Text;
            string 班组 = uiTextBox班组.Text;
            string 颜色 = uiTextBox颜色.Text;

            if (string.IsNullOrEmpty(仓库))
            {
                //MessageBox.Show("请选择仓库");
            }
            if (string.IsNullOrEmpty(包装形式))
            {
                MessageBox.Show("请选择包装形式");
            }

            //如果有打印了，是要覆盖重新打印，还是不打印
            //酸洗
            string sqlqueryPicklingOutput = @"select OutputVolumeNew 
                        from Bus_PicklingOutput
                        where OutputVolumeNew like '%"+ this.uiTextBox生产批号.Text.Trim().Replace("--", "-") + "%'";
            DataTable dtPicklingOutput = pub.GetDataTable(pub.ConnectionString,sqlqueryPicklingOutput);
            if (dtPicklingOutput != null && dtPicklingOutput.Rows.Count > 0) 
            {
                MessageBox.Show("该卷已经通过正常酸洗报工流程，不可以临时打印");
                return;
            }

            //罩退
            string sqlCoverFire = @"select OutputVolumeNew 
                                    from Bus_CoverFireOutput
                                    where OutputVolumeNew like '%"+ this.uiTextBox生产批号.Text.Trim().Replace("--", "-") + "%'";
            DataTable dtCoverFire = pub.GetDataTable(pub.ConnectionString, sqlCoverFire);
            if (dtCoverFire != null && dtCoverFire.Rows.Count > 0)
            {
                MessageBox.Show("该卷已经通过正常罩退报工流程，不可以临时打印");
                return;
            }

            //退火
            string sqlFireOutput = @"select OutputVolumeNew 
                            from [dbo].Bus_FireOutput
                            where OutputVolumeNew like '%" + this.uiTextBox生产批号.Text.Trim().Replace("--", "-") + "%'";
            DataTable dtFireOutput  = pub.GetDataTable(pub.ConnectionString, sqlFireOutput);
            if (dtFireOutput != null && dtFireOutput.Rows.Count > 0)
            {
                MessageBox.Show("该卷已经通过正常退火报工流程，不可以临时打印");
                return;
            }

            //轧硬
            string sqlHardOutput = @"select OutputVolumeNew 
                                    from [dbo].Bus_HardOutput
                                    where OutputVolumeNew like '%" + this.uiTextBox生产批号.Text.Trim().Replace("--", "-") + "%'";
            DataTable dtHardOutput = pub.GetDataTable(pub.ConnectionString, sqlHardOutput);
            if (dtHardOutput != null && dtHardOutput.Rows.Count > 0)
            {
                MessageBox.Show("该卷已经通过正常轧硬报工流程，不可以临时打印");
                return;
            }

            //喷涂
            string sqlPaintOutput = @"select OutputVolumeNew 
                                    from [dbo].Bus_PaintOutput
                                    where OutputVolumeNew like  '%" + this.uiTextBox生产批号.Text.Trim().Replace("--", "-") + "%'";
            DataTable dtPaintOutputt = pub.GetDataTable(pub.ConnectionString, sqlPaintOutput);
            if (dtPaintOutputt != null && dtPaintOutputt.Rows.Count > 0)
            {
                MessageBox.Show("该卷已经通过正常喷涂报工流程，不可以临时打印");
                return;
            }

            //硅钢
            string sqlSiOutput = @"select OutputVolumeNew 
                                    from [dbo].Bus_SiOutput
                                    where OutputVolumeNew like  '%" + this.uiTextBox生产批号.Text.Trim().Replace("--", "-") + "%'";
            DataTable dtSiOutput = pub.GetDataTable(pub.ConnectionString, sqlSiOutput);
            if (dtSiOutput != null && dtSiOutput.Rows.Count > 0)
            {
                MessageBox.Show("该卷已经通过正常硅钢报工流程，不可以临时打印");
                return;
            }

            //镀锌
            string sqlZincOutput = @"select OutputVolumeNew 
                                    from [dbo].Bus_ZincOutput
                                    where OutputVolumeNew like  '%" + this.uiTextBox生产批号.Text.Trim().Replace("--", "-") + "%'";
            DataTable dtZincOutput = pub.GetDataTable(pub.ConnectionString, sqlZincOutput);
            if (dtZincOutput != null && dtZincOutput.Rows.Count > 0)
            {
                MessageBox.Show("该卷已经通过正常镀锌报工流程，不可以临时打印");
                return;
            }



            生产批号 = uiLabel前缀.Text+this.uiTextBox生产批号.Text.Trim().Replace("--", "-");
            sql = "SELECT  生产批号 ";
            sql += " from  Bus_LablePrint where 生产批号='" + 生产批号 + "'";
            sql += " union all ";
            sql += " SELECT  VolumeCodeNew   ";
            sql += " from  Bus_StockInVolume where VolumeCodeNew='" + 生产批号 + "'";

            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                MessageBox.Show(生产批号 + "已经打印过了，请联系一下管理员处理", "提示", MessageBoxButtons.OK);
                return;
            }

            生产日期 = this.uiDatePicker报工日期.Value.ToString("yyyy-MM-dd");
            品名 = this.uiComboBox品名.Text.Trim().Replace("--", "");
            if (品名 == "")
            {
                MessageBox.Show("品名有问题", "提示", MessageBoxButtons.OK);
                return;
            }
            钢号 = this.uiComboBox钢号.SelectedValue.ToString();
            规格 = this.uiTextBox规格.Text.Trim().Replace("--", "");
            if (!pub.IsRightSize(规格))
            {
                MessageBox.Show("规格输入格式有问题", "提示", MessageBoxButtons.OK);
                return;
            }
            等级 = this.uiComboBox等级.SelectedValue.ToString();
            表面状态 = this.uiTextBox表面状态.Text.Trim().Replace("--", "");
            表面处理 = this.uiComboBox表面处理.Text.Trim().Replace("--", ""); 
            净重 = pub.Todecimal(this.uiTextBox净重.Text, 0);
            if (净重 == "0")
            {
                MessageBox.Show("净重有问题", "提示", MessageBoxButtons.OK);
                return;
            }
            长度 = pub.Todecimal(this.uiTextBox长度.Text, 0);
            if (长度 == "0")
            {
                MessageBox.Show("长度有问题", "提示", MessageBoxButtons.OK);
                return;
            }
            备注 = this.uiTextBox备注.Text.Trim().Replace("--", "");

            //BarTender.Application ba = new BarTender.Application();
            //BarTender.Format bfmt;
            //bfmt = new BarTender.Format();
            //switch (uiComboBox加工类别.Text)
            //{
            //    case "镀锌":
            //        bfmt = ba.Formats.Open(System.Windows.Forms.Application.StartupPath + "\\攀华镀锌小标签.btw");
            //        break;
            //    case "彩涂":
            //        bfmt = ba.Formats.Open(System.Windows.Forms.Application.StartupPath + "\\攀华彩涂小标签.btw");
            //        break;
            //    case "冷轧":
            //        bfmt = ba.Formats.Open(System.Windows.Forms.Application.StartupPath + "\\攀华罩退小标签.btw");
            //        break;
            //    case "彩基板":
            //        bfmt = ba.Formats.Open(System.Windows.Forms.Application.StartupPath + "\\攀华镀锌小标签.btw");
            //        break;

            //}
            //打印打标签
            bfmt = ba.Formats.Open(System.Windows.Forms.Application.StartupPath + "\\攀华大标签.btw");
            bfmt.SetNamedSubStringValue("生产批号", 生产批号);
            bfmt.SetNamedSubStringValue("品名", 品名);
            bfmt.SetNamedSubStringValue("钢号", 钢号);
            bfmt.SetNamedSubStringValue("长度", 长度);
            bfmt.SetNamedSubStringValue("规格", 规格);
            bfmt.SetNamedSubStringValue("等级", 等级);
            bfmt.SetNamedSubStringValue("表面状态", 表面状态);
            bfmt.SetNamedSubStringValue("表面处理", 表面处理);
            bfmt.SetNamedSubStringValue("净重", 净重);
            bfmt.SetNamedSubStringValue("备注", 备注);
            bfmt.SetNamedSubStringValue("生产日期", 生产日期);
            //临时打印没有班组
            //bfmt.SetNamedSubStringValue("班组", 班组);
            //bfmt.SetNamedSubStringValue("仓库", 仓库);

            bfmt.PrintOut();
            sql = "delete Bus_LablePrint where 生产批号='" + 生产批号 + "'";
            pub.ExecuteSql(pub.ConnectionString, sql, "临时小标签打印前删除");
            sql = "insert into Bus_LablePrint(guid,来源种类,来源产线, 工厂编号, 生产批号,  品名, 钢号, 条码, 长度, 规格, 等级, 表面状态,  ";
            sql += " 表面处理, 净重, 备注, 生产日期,  操作ip, 操作人员, 操作时间,仓库,锌层,包装形式,班组,颜色 ) ";
            sql += " values(newid(),'"+"临时"+ uiComboBox加工类别.Text+"小标签" + "','"+ this.uiComboBox产线.Text  +"','" + this.uiComboBoxWorkshop.SelectedValue.ToString() + "','" + 生产批号 + "',";
            sql += " '" + 品名 + "','" + 钢号 + "','" + 生产批号 + "','" + 长度 + "','" + 规格 + "','" + 等级 + "','" + 表面状态 + "',";
            sql += " '" + 表面处理 + "','" + 净重 + "','" + 备注 + "','" + 生产日期 + "','" 
                + pub.getMac() + "','" + pub.sys_user.UserTrueName + "',getdate(),'"+ 仓库 + "','"+ 锌层 + "','"+ 包装形式 + "','"+ 班组 + "','"+ 颜色 + "')";
            pub.ExecuteSql(pub.ConnectionString, sql, "临时小标签打印塞入");
            //ba.Quit();
            未打印();
        }

        private void uiDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1 && e.ColumnIndex == 0)
            {
                if (MessageBox.Show("确定删除？", "警告", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    string 填报人 = this.uiDataGridView1.Rows[e.RowIndex].Cells["C操作人员"].Value.ToString();
                    string VolumeCodeNew = this.uiDataGridView1.Rows[e.RowIndex].Cells["C卷号"].Value.ToString();
                    if (填报人 != pub.sys_user.UserTrueName)
                    {
                        MessageBox.Show("您不可以删除其他人员填报的数据", "提示", MessageBoxButtons.OK);
                        return;
                    }
                    //不能删除已经打印大标签的
                    string sql = "select VolumeCodeNew from Bus_StockInVolume where VolumeCodeNew ='" + VolumeCodeNew + "'";
                    sql += "union all ";
                    sql += " select VolumeCodeNew from Bus_Volume where VolumeCodeNew ='" + VolumeCodeNew + "'";
                    DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
                    if (tb.Rows.Count>0 )
                    {
                        MessageBox.Show("您不可以删除已经入库的临时小标签，请联系管理员", "提示", MessageBoxButtons.OK);
                        return;
                    }
                    DataTable dt = uiDataGridView1.DataSource as DataTable;
                    string guid = uiDataGridView1.Rows[e.RowIndex].Cells["Cguid"].Value.ToString();
                    sql = "delete from Bus_LablePrint where guid = '"+ guid + "'";
                    int r = pub.ExecuteSql(pub.ConnectionString,sql,"删除临时小标签");
                    未打印();
                }
               

            }
        }

        private void uiComboBox品名_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void uiLabel19_Click(object sender, EventArgs e)
        {

        }
        

        private void uiButton刷新_Click(object sender, EventArgs e)
        {
            //未打印();
            盘点入库();
        }

        private void 盘点入库()
        {
            string[] New_MSG = uiTextBox1.Text.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            string sql = @"select *       
                            from Bus_LablePrint
                            where 生产批号 in
                            ('{0}')
                        and 生产批号 not in
                        (select VolumeCodeNew from Bus_StockInVolume)";
            sql = string.Format(sql, string.Join("','", New_MSG));
            DataTable dt = pub.GetDataTable(pub.ConnectionString, sql);
            uiDataGridView1.DataSource = dt;
            uiDataGridView1.Refresh();
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    
                    string 生产批号 = dt.Rows[i]["生产批号"].ToString();
                    string 来源种类 = dt.Rows[i]["来源种类"].ToString();
                    string Customer = dt.Rows[i]["客户"].ToString();
                    string 品名 = dt.Rows[i]["品名"].ToString();
                    string 钢号 = dt.Rows[i]["钢号"].ToString();
                    string 规格 = dt.Rows[i]["规格"].ToString();
                    string 工厂编号 = dt.Rows[i]["工厂编号"].ToString();
                    string 来源产线 = dt.Rows[i]["来源产线"].ToString();

                    string 厚度 = dt.Rows[i]["厚度"].ToString();
                    string 长度 = dt.Rows[i]["长度"].ToString();
                    string 颜色 = dt.Rows[i]["颜色"].ToString();
                    //string 规格 = dt.Rows[e.RowIndex]["规格"].ToString();
                    string 等级 = dt.Rows[i]["等级"].ToString();
                    string 表面状态 = dt.Rows[i]["表面状态"].ToString();
                    string 表面处理 = dt.Rows[i]["表面处理"].ToString();
                    string 净重 = dt.Rows[i]["净重"].ToString();
                    string 备注 = dt.Rows[i]["备注"].ToString();
                    string 生产日期 = dt.Rows[i]["生产日期"].ToString();
       
                    string 仓库 = dt.Rows[i]["仓库"].ToString();
                    string 货位 = dt.Rows[i]["货位"].ToString();
                    string 锌层 = 表面状态;
                    string 包装形式 = dt.Rows[i]["包装形式"].ToString();
                   

                    string docCode = deleteBus_StockInV(工厂编号, 生产批号, 生产批号, "", 品名, 钢号, 长度, 厚度, 规格, 等级, 表面状态, 表面处理, 净重, 备注, 生产日期, "万达", 锌层, 包装形式, Customer, 仓库, 来源产线, 颜色);
                   
                }
            }

            sql = @"insert into Bus_Volume( [guid], [WorkShopCode],[VolumeCodeNew], [FinishSize], [InventoryCode], [InventoryName], [Material], [FaceStatus], [FaceDone], [FinishWeight], [Meter], [FinishRank], [ProductDate], [StorageLocation], [IsInStorage], [Customer], [Address], [WarehouseCode], [ZinCeng], [PackageType],Color)
                            select newid(),		[工厂编号]		,[生产批号],		[规格],    [存货编号],       [品名],			[钢号],   [表面状态], [表面处理]	, [净重], [长度], [等级], [生产日期],		[货位],					'在库'		, [客户], [产地], [仓库], [锌层],[包装形式],[颜色]
                            from Bus_LablePrint
                            where 生产批号 in ('{0}')
                            and 生产批号 not in (select [VolumeCodeNew] from Bus_Volume)";
            sql = string.Format(sql, string.Join("','", New_MSG));

            int r = pub.ExecuteSql(pub.ConnectionString, sql);


        }

        private string deleteBus_StockInV(string 工厂编号, string 生产批号, string 存货编号, string departCode, string 品名, string 钢号, string 长度, string 厚度, string 规格, string 等级, string 表面状态, string 表面处理, string 净重, string 备注, string 生产日期, string 产地, string 锌层, string 包装形式, string Customer, string 仓库, string 来源产线, string 颜色)
        {
            //查出 入库单
            string sqlqueryIsInstock = @"select VolumeCodeNew,Bus_StockInV.DocCode,Bus_StockInV.guid
                                        from Bus_StockInV
                                        left join Bus_StockInVolume on Bus_StockInV.guid = Bus_StockInVolume.guid
                                                where VolumeCodeNew =  '" + 生产批号 + "'";
            DataTable dtIsInstock = pub.GetDataTable(pub.ConnectionString, sqlqueryIsInstock);
            if (dtIsInstock != null && dtIsInstock.Rows.Count > 0)
            {
                string oldguid = dtIsInstock.Rows[0]["guid"].ToString();
                //删除
                string sqlDeleteStockV = "delete from Bus_StockInV where guid = '" + oldguid + "'";
                int deleStockV = pub.ExecuteSql(pub.ConnectionString, sqlDeleteStockV, "删除入库单重新生成");
                if (deleStockV < 1)
                {
                    MessageBox.Show("删除入库单头重新生成失败");
                }
                string sqlDeleteStockVolume = "delete from Bus_StockInVolume where guid = '" + oldguid + "'";
                int deleStockVolume = pub.ExecuteSql(pub.ConnectionString, sqlDeleteStockVolume, "删除入库单重新生成");
                if (deleStockVolume < 1)
                {
                    MessageBox.Show("删除入库单体重新生成失败");
                }
            }

            string guid = Guid.NewGuid().ToString();
            string Bguid = Guid.NewGuid().ToString();
            string docCode = pub.GetDocCode("产品入库", 工厂编号);
            docCode += "$" + guid;
            decimal.TryParse(厚度, out decimal a);
            string sql;
            sql = " insert into Bus_StockInV(guid, StockInType, WorkShopCode, DocCode,  DepartCode, CreateID, Creator, CreateDate,IsSendToNC)";
            sql += " values('" + guid + "','产品入库 ','" + 工厂编号 + "','" + docCode + "', '" + departCode + "','" + pub.sys_user.User_Id + "','" + pub.sys_user.UserTrueName + "', getdate(),'Y');";

            sql += @" insert into Bus_StockInVolume
                        (guid,Bguid, DocCode, RowNumber, WorkShopCode, WarehouseCode, StorageLocation, VolumeCodeNew, InventoryCode, Size, 
                        Thick,  Material,CID,Virtual,FaceStatus,FaceDone, Weight, Address,ProductDate,Grade,Meter,[ZinCeng],PackageType,Customer,WorkLine, PingMing,Color ) values ('" +
                        guid + "','" + Bguid + "', '" + docCode + "', '1', '" + 工厂编号 + "', '" + 仓库 + "', '', '" + 生产批号 + "', '" + 存货编号 + "', '" + 规格 + "', '"
                        + a + "',  '" + 钢号 + "','','C','" + 表面状态 + "',' " + 表面处理 + "','" + 净重 + "', '" + 产地 + "','" + 生产日期 + "','" + 等级 + "','" + 长度 + "',";
            sql += "'" + 锌层 + "','" + 包装形式 + "','" + Customer + "','" + 来源产线 + "','" + 品名 + "','" + 颜色 + "');";
            int r = pub.ExecuteSql(pub.ConnectionString, sql, "保存成品入库单据");
            string sqlqueryHeader = "select * from Bus_StockInV where guid='" + guid + "' ";
            DataTable tbr = pub.GetDataTable(pub.ConnectionString, sqlqueryHeader);
            if (tbr.Rows.Count > 0)
            {
                string sqlqueryBody = "select * from Bus_StockInVolume where guid='" + guid + "'";
                DataTable tbb = pub.GetDataTable(pub.ConnectionString, sqlqueryBody);
                if (tbb.Rows.Count > 0)
                {
                    //MessageBox.Show("保存成功单号【" + docCode + "】!", "提示", MessageBoxButtons.OK);

                    return docCode;
                }
                else
                {
                    sql = "delete  Bus_StockInV  where guid='" + guid + "'";
                    pub.ExecuteSql(pub.ConnectionString, sql, "因为保存失败了，删除原料入库单");
                    MessageBox.Show("保存失败,代码【err1】!", "提示", MessageBoxButtons.OK);
                    return docCode;
                }
            }
            else
            {
                sql = "delete  Bus_StockInVolume  where guid='" + guid + "'";

                pub.ExecuteSql(pub.ConnectionString, sql, "因为保存失败了，删除原料入库单");
                MessageBox.Show("保存失败,代码【err2】!", "提示", MessageBoxButtons.OK);
                return docCode;
            }
        }


        private void uiButton导出_Click(object sender, EventArgs e)
        {

        }

        private void uiComboBox产线_SelectedIndexChanged(object sender, EventArgs e)
        {
            string workLineCode;
            if (this.uiComboBox产线.SelectedValue.ToString() != "")
            {
                workLineCode = this.uiComboBox产线.SelectedValue.ToString();
                pub.全局工厂编号 = pub.全局工厂编号;
                Dic_WorkLine dic_workline = pub.GetWorkLineByLineCode(pub.全局工厂编号, workLineCode);
                //if (dic_workline.DepartCode != null)
                //{
                //    Dic_Depart dic_depart = pub.GetDepartByCode(dic_workline.DepartCode, pub.全局工厂编号);
                //    if (dic_depart.DepartCode != "")
                //    {
                //        this.uiTextBox部门1.Text = dic_depart.DepartCode;
                //        this.uiTextBox部门名称1.Text = dic_depart.DepartName;
                //    }
                //    else
                //    {
                //        this.uiTextBox部门1.Text = "";
                //        this.uiTextBox部门名称1.Text = "";
                //    }
                //}
                //else
                //{
                //    this.uiTextBox部门1.Text = "";
                //    this.uiTextBox部门名称1.Text = "";
                //}

                //this.uiComboBox仓库编号1.SelectedValue = pub.根据产线获得仓库编号(this.uiComboBox产线.SelectedText);
                //pub.ListWorkType(pub.全局工厂编号, FormWorkType, this.uiComboBox去向);
                //pub.ListWorkTeam(pub.全局工厂编号, workLineCode, this.uiComboBoxWorkTeam);
            }
        }

        private void uiButton导入_Click(object sender, EventArgs e)
        {
            try
            {
                string path;
                //openFileDialog1.InitialDirectory = Application.ExecutablePath;
                //openFileDialog1.Filter = "Excel文件|*.xlsx";
                //openFileDialog1.FilterIndex = 2;
                openFileDialog1.RestoreDirectory = true;

                this.openFileDialog1.Filter = "Excel 文件(*.xlsx)|*.xlsx";
                if (this.openFileDialog1.ShowDialog(this) == DialogResult.OK)
                {
                    path = this.openFileDialog1.FileName;
                    DataTable table = (DataTable)MiniExcel.QueryAsDataTable(path, useHeaderRow: true);
                    //检查有没有导入过
                    if (table != null && table.Rows.Count > 0)
                    {
                        string[] 卷号Arrary = table.AsEnumerable().Select(row => "'" + row.Field<string>("卷号") +"'").ToArray();
                        string para = string.Join(",", 卷号Arrary);
                        string sql = "select 生产批号 from Bus_LablePrint where 生产批号 in ("+ para + ")";
                        DataTable dt = pub.GetDataTable(pub.ConnectionString,sql);
                        if (dt.Rows.Count > 0)
                        {
                            MessageBox.Show("卷号有重复，请检查");
                            return;
                        }
                        else
                        {
                            for (int i = 1; i < table.Rows.Count; i++)
                            {
                                string 存货编号 = table.Rows[i][0].ToString();
                                string 部门 = table.Rows[i][1].ToString();
                                string 品名 = table.Rows[i][2].ToString();
                                string 规格 = table.Rows[i][3].ToString();
                                string 生产批号 = table.Rows[i][4].ToString();
                                string 净重 = table.Rows[i][5].ToString();
                                string 长度 = table.Rows[i][6].ToString();
                                string 钢号 = table.Rows[i][7].ToString();
                                string 仓库 = table.Rows[i][8].ToString();
                                string 入库日期 = table.Rows[i][9].ToString();
                                string 生产日期 = table.Rows[i][10].ToString();
                                string 颜色 = table.Rows[i][11].ToString();
                                string 产地 = table.Rows[i][12].ToString();
                                string 表面状态 = table.Rows[i][13].ToString().Replace("常规","Z");
                                string 厚度 = table.Rows[i][14].ToString();
                                string 货位 = table.Rows[i][15].ToString();
                                string 次级库位 = table.Rows[i][16].ToString();
                                string 船名 = table.Rows[i][17].ToString();
                                string 备注 = table.Rows[i][18].ToString();
                                string 等级 = "";
                                if (品名.ToUpper().Contains("B"))
                                {
                                    等级 = "B";
                                }
                                else
                                {
                                    等级 = "A";
                                }
                                净重 = (decimal.Parse(净重) * 1000).ToString();
                                string 表面处理 = "L";
                                string sqlInsert = "insert into Bus_LablePrint ([guid], [来源种类],   [工厂编号], [生产批号], [存货编号], [品名], [钢号], [条码], [厚度], [产地], [长度], [规格], [等级], [表面状态], [表面处理], [净重], [备注], [生产日期], [检验员], [操作ip], [操作人员], [操作时间], [仓库], [货位], [锌层], [包装形式], [班组], [客户], [颜色], [是否已打印], [打印次数], [是否抛送NC], [抛NC结果], [抛NC时间], [抗拉], [屈服], [延伸率])";
                                sqlInsert +=                         "values( newid(), '期初打印',   '"+pub.全局工厂编号+"', '"+ 生产批号 + "', '"+ 存货编号 + "', '"+ 品名 + "', '"+ 钢号 + "', '"+ 生产批号 + 
                                                "', '"+ 厚度 + "', '"+产地+"', '"+ 长度 + "', '"+ 规格 + "',  '"+ 等级 + "', '"+ 表面状态 + "', '"+ 表面处理 + "', '"+ 净重 + 
                                                "', '"+ 备注 + "', '"+ 生产日期 + "', '', '', '"+pub.sys_user.UserName+ "', getdate(), '"+ 仓库 + "', '"+ 货位 + "', '', '', '',  '', '"+ 颜色 + "', '否', '0', 'N', '', '', '', '', '')";
                                int R = pub.ExecuteSql(pub.ConnectionString,sqlInsert,"");
                            }
                        }
                    }
                }
                未打印();
            }
            catch
            {

            }
        }

        private void Form张家港标签临时打印_FormClosed(object sender, FormClosedEventArgs e)
        {
            ba.Quit();
        }

        private void uiButton全选_Click(object sender, EventArgs e)
        {
            if (uiDataGridView1 != null && uiDataGridView1.Rows != null && uiDataGridView1.Rows.Count > 0)
            {
                for (int i = 0; i < uiDataGridView1.Rows.Count; i++)
                {//选择了就取消 没选择就选择
                    this.uiDataGridView1.Rows[i].Cells["C选择"].Value = true;
                }
            }
        }

        private void uiButton全不选_Click(object sender, EventArgs e)
        {
            if (uiDataGridView1 != null && uiDataGridView1.Rows != null && uiDataGridView1.Rows.Count > 0)
            {
                for (int i = 0; i < uiDataGridView1.Rows.Count; i++)
                {//选择了就取消 没选择就选择
                    this.uiDataGridView1.Rows[i].Cells["C选择"].Value = false;
                }
            }
        }

        private void uiButton打印标签_Click(object sender, EventArgs e)
        {
            if (uiDataGridView1 != null && uiDataGridView1.Rows != null && uiDataGridView1.Rows.Count > 0)
            {
                for (int i = 0; i < uiDataGridView1.Rows.Count; i++)
                {//选择了就取消 没选择就选择
                    if (this.uiDataGridView1.Rows[i].Cells["C选择"].Value != null && (bool)uiDataGridView1.Rows[i].Cells["C选择"].Value)
                    {
                        
                        string 生产批号 = uiDataGridView1.Rows[i].Cells["C卷号"].Value.ToString();
                        string 品名 = uiDataGridView1.Rows[i].Cells["C品名"].Value.ToString();
                        string 钢号 = uiDataGridView1.Rows[i].Cells["C钢号"].Value.ToString();
                        string 长度 = uiDataGridView1.Rows[i].Cells["C长度"].Value.ToString();
                        string 规格 = uiDataGridView1.Rows[i].Cells["C规格"].Value.ToString();
                        string 等级 = uiDataGridView1.Rows[i].Cells["C等级"].Value.ToString();
                        string 表面状态 = uiDataGridView1.Rows[i].Cells["C表面状态"].Value.ToString();
                        string 表面处理 = uiDataGridView1.Rows[i].Cells["C表面处理"].Value.ToString();
                        string 颜色 = uiDataGridView1.Rows[i].Cells["C颜色"].Value.ToString();
                        string 净重 = uiDataGridView1.Rows[i].Cells["C净重"].Value.ToString();
                        string 备注 = uiDataGridView1.Rows[i].Cells["C备注"].Value.ToString();
                        string 生产日期 = uiDataGridView1.Rows[i].Cells["C生产日期"].Value.ToString();
                        if (品名.Contains("彩涂"))
                        {
                            print(生产批号, 品名, 钢号, 长度, 规格, 等级, 表面状态, 颜色, 净重, 备注, 生产日期);
                            string sqlupdate = "update [Bus_LablePrint] set 是否已打印 = '是' where 生产批号 = '" + 生产批号 + "'";
                            int r = pub.ExecuteSql(pub.ConnectionString, sqlupdate, "");
                        }
                        else
                        {
                            print(生产批号, 品名, 钢号, 长度, 规格, 等级, 表面状态, 表面处理, 净重, 备注, 生产日期);
                            string sqlupdate = "update [Bus_LablePrint] set 是否已打印 = '是' where 生产批号 = '" + 生产批号 + "'";
                            int r = pub.ExecuteSql(pub.ConnectionString, sqlupdate, "");
                        }
                        
                    }
                }
            }
           
        }

        private void print(string 生产批号, string 品名, string 钢号, string 长度, string 规格, string 等级, string 表面状态, string 表面处理, string 净重, string 备注, string 生产日期)
        {
            //打印打标签
            bfmt = ba.Formats.Open(System.Windows.Forms.Application.StartupPath + "\\攀华大标签.btw");
            bfmt.SetNamedSubStringValue("生产批号", 生产批号);
            bfmt.SetNamedSubStringValue("品名", 品名);
            bfmt.SetNamedSubStringValue("钢号", 钢号);
            bfmt.SetNamedSubStringValue("长度", 长度);
            bfmt.SetNamedSubStringValue("规格", 规格);
            bfmt.SetNamedSubStringValue("等级", 等级);
            bfmt.SetNamedSubStringValue("表面状态", 表面状态);
            bfmt.SetNamedSubStringValue("表面处理", 表面处理);
            bfmt.SetNamedSubStringValue("净重", 净重);
            bfmt.SetNamedSubStringValue("备注", 备注);
            bfmt.SetNamedSubStringValue("生产日期", 生产日期);
            //临时打印没有班组
            //bfmt.SetNamedSubStringValue("班组", 班组);
            //bfmt.SetNamedSubStringValue("仓库", 仓库);

            bfmt.PrintOut();
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            decimal 总重量;
            decimal 总长度;
            int 总卷数;
            总重量 = 0;
            总长度 = 0;
            string[] New_MSG =  uiTextBox1.Text.Split(new string[] { "\r\n" }, StringSplitOptions.None);

            string sql = @"select guid,来源种类,来源产线, 工厂编号, 生产批号,  品名, 锌层,钢号,包装形式, 条码, 长度, 规格,班组, 等级, 表面状态, 仓库 , 表面处理,";
            sql += "    净重 ,'' 文本净重, 备注, 生产日期,操作人员,操作时间,颜色 from Bus_LablePrint where 1=1 and 生产批号 in ('{0}')";
            sql = string.Format(sql, string.Join("','", New_MSG));
            DataTable dt = pub.GetDataTable(pub.ConnectionString,sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    decimal 净重 = pub.Todecimal(dt.Rows[i]["净重"].ToString());
                    dt.Rows[i]["文本净重"] = Math.Round(净重, 0).ToString();
                    总重量 += 净重;
                    总长度 += pub.Todecimal(dt.Rows[i]["长度"].ToString());
                }
            }
            uiDataGridView1.DataSource = dt;
            uiDataGridView1.Refresh();
            this.uiTextBox总卷数.Text = dt.Rows.Count.ToString();
            this.uiTextBox总重量.Text = Math.Round(总重量, 0).ToString();
            this.uiTextBox总米数.Text = 总长度.ToString();
        }
    }
}
